/*
 * $Id: FileUploadValidator.java,v 1.6 2012/02/01 20:07:08 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.onwatch.bean.FileUploadBean;
import com.ge.healthcare.autosc.onwatch.util.OnWatchConstants;
/**
 * 
 * @author 212042946
 * FileUploadValidator validates file upload request from the remote system.
 * The scope of validation in this class limits to the existence of required
 * data in the request to satisfy requested operation. It does not include the 
 * validation of the data quality to the requested operation.
 */
public class FileUploadValidator extends BasePostValidator {

	public boolean supports(Class clazz) {
		return clazz.equals(FileUploadBean.class);
	}

	public void validate(Object command, Errors errors) {
		super.validate(command, errors);

		FileUploadBean fileUploadBean = (FileUploadBean)command;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "checksum", "required.checksum",
				"Checksum is required");
		validateRequestType(fileUploadBean, errors);
		validateFieldsByRequestType(fileUploadBean, errors);
	}

	protected void validateRequestType(FileUploadBean bean, Errors errors) {
		if((!OnWatchConstants.PRODIAGS_REQUEST_TYPE.equalsIgnoreCase(bean.getRequestType())) &&
		   (!OnWatchConstants.STATUSREPORT_REQUEST_TYPE.equalsIgnoreCase(bean.getRequestType()))){
			errors.reject("invalid.requestType", "Invalid request");
		}
	}
	
	private void validateFieldsByRequestType(FileUploadBean fileUploadBean, Errors errors) {
		String methodName = "validateNewRequest";
		
		if(OnWatchConstants.PRODIAGS_REQUEST_TYPE.equalsIgnoreCase(fileUploadBean.getRequestType())) {
			ASCLogger.debug(this.getClass(), methodName, "validate createdBy");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "createdBy", "required.createdBy",
					"Created By is required");
			ASCLogger.debug(this.getClass(), methodName, "validate serviceId");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "serviceId", "required.serviceId",
					"Service Id is required");
/*			ASCLogger.debug(this.getClass(), methodName, "validate serviceDate");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "serviceDate", "required.serviceDate",
					"Service date is required");
*/
		} 
	}
}
